{
  "name": "Case 42: Foreign Exchange (FX) Exposure Analyzer",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "value": "1BsTawDoLABHuBqLdblVsQKo56DNKvphc",
          "mode": "list",
          "cachedResultName": "Case 42: Foreign Exchange Invoice Folder",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1BsTawDoLABHuBqLdblVsQKo56DNKvphc"
        },
        "event": "fileCreated",
        "options": {}
      },
      "id": "a2bcc505-7cfa-4ed7-8b3e-5891260b867f",
      "name": "Invoice Upload Trigger",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "typeVersion": 1,
      "position": [
        -816,
        0
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "Cf6MNES8JrQntp3r",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "value": "={{ $json.id }}",
          "mode": "id"
        },
        "options": {}
      },
      "id": "387b7d8a-9dae-4742-b2e2-d00d8e2f21c1",
      "name": "Download Invoice",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -624,
        0
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "Cf6MNES8JrQntp3r",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.cloud.llamaindex.ai/api/v1/parsing/upload",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer llx-7BWYrvPwfUhgdXbZMtaNCsr2O0h7JH4dkHjHisxcvq46bkIj"
            }
          ]
        },
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "parameterType": "formBinaryData",
              "name": "file",
              "inputDataFieldName": "data"
            }
          ]
        },
        "options": {}
      },
      "id": "3884ae2a-1d64-41da-95d4-12a39d0da448",
      "name": "LlamaIndex Upload",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -416,
        0
      ]
    },
    {
      "parameters": {
        "amount": 20
      },
      "id": "f6207c4d-4dc1-4e04-b8bc-6593d6fd6a7e",
      "name": "Wait for Parsing",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        -224,
        0
      ],
      "webhookId": "03aeee15-d2af-458d-9f2f-d2abfd106cb6"
    },
    {
      "parameters": {
        "url": "=https://api.cloud.llamaindex.ai/api/v1/parsing/job/{{ $node[\"LlamaIndex Upload\"].json[\"id\"] }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer llx-7BWYrvPwfUhgdXbZMtaNCsr2O0h7JH4dkHjHisxcvq46bkIj"
            }
          ]
        },
        "options": {}
      },
      "id": "e8f5a593-378c-43e4-8082-8ae7bf5fd0e5",
      "name": "Check Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -16,
        0
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "SUCCESS",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "84cec710-36bd-41c2-81ba-58a06d457eba"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "SUCCESS"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "ef27da0f-4166-4d96-98c8-49dad88c9b50",
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "PENDING",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "PENDING"
            }
          ]
        },
        "options": {}
      },
      "id": "c53e104d-f3ea-4342-bab1-482b55cd1548",
      "name": "Status Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.3,
      "position": [
        192,
        0
      ]
    },
    {
      "parameters": {
        "url": "=https://api.cloud.llamaindex.ai/api/v1/parsing/job/{{ $node[\"LlamaIndex Upload\"].json[\"id\"] }}/result/markdown",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer llx-7BWYrvPwfUhgdXbZMtaNCsr2O0h7JH4dkHjHisxcvq46bkIj"
            }
          ]
        },
        "options": {}
      },
      "id": "51eda38b-0666-4615-b1c9-fa8c0153252e",
      "name": "Get Markdown",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        384,
        -112
      ]
    },
    {
      "parameters": {
        "text": "={{ $json.markdown }}",
        "attributes": {
          "attributes": [
            {
              "name": "Invoice Number",
              "description": "The unique invoice ID (e.g., TFS-2025-9942)."
            },
            {
              "name": "Supplier Name",
              "description": "Legal name of the sender (e.g., TechFlow Systems GmbH)."
            },
            {
              "name": "Invoice Date",
              "description": "Date of issue in YYYY-MM-DD format."
            },
            {
              "name": "Currency",
              "description": "The 3-letter currency code (e.g., EUR, USD)."
            },
            {
              "name": "Base Amount (Original)",
              "description": "Total numeric value before tax in original currency."
            }
          ]
        },
        "options": {}
      },
      "id": "01b856ac-605d-431c-a2da-29854ab0208c",
      "name": "FX Invoice Extractor",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "typeVersion": 1.2,
      "position": [
        592,
        -112
      ]
    },
    {
      "parameters": {
        "url": "=https://api.freecurrencyapi.com/v1/latest?apikey=fca_live_eZ4DDrYpwdeXydfs8jEsRykKdQPiXSmLlWSCrJfh&base_currency={{ $node[\"FX Invoice Extractor\"].json.output.Currency }}&currencies=INR",
        "options": {}
      },
      "id": "3b4dfa1b-44fc-4a04-b4c5-408015ff4c94",
      "name": "Fetch Real-time FX",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        912,
        -112
      ]
    },
    {
      "parameters": {
        "jsCode": "// Get data from previous nodes\nconst out = $node[\"FX Invoice Extractor\"].json.output;\nconst rate = $node[\"Fetch Real-time FX\"].json.data.INR;\n\n// CLEANING: Remove currency symbols (€, $, £) and commas from the amount string\n// Then convert it to a floating-point number\nconst rawAmount = out[\"Base Amount (Original)\"].toString().replace(/[^\\d.-]/g, '');\nconst cleanAmount = parseFloat(rawAmount);\n\nreturn [{\n  json: {\n    \"Invoice Number\": out[\"Invoice Number\"],\n    \"Supplier Name\": out[\"Supplier Name\"],\n    \"Invoice Date\": out[\"Invoice Date\"],\n    \"Currency\": out[\"Currency\"],\n    \"Base Amount (Original)\": cleanAmount, // Now a clean number\n    \"FX Rate (to INR)\": parseFloat(rate).toFixed(2),\n    \"Total Value (INR)\": (cleanAmount * rate).toFixed(2), // Calculation will now work\n    \"Treasury Approval Status\": \"Pending\",\n    \"Risk Category\": (cleanAmount * rate) > 100000 ? \"High\" : \"Medium\"\n  }\n}];"
      },
      "id": "594899a5-7d48-4b45-8692-c04686c82d39",
      "name": "FX Calculator",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1104,
        -112
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1hbwjEAShTXNv2FaKQ5OiYjCItRwuob3S9cNBJliAe68",
          "mode": "list",
          "cachedResultName": "Case_42_Treasury_FX_Exposure_Log",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1hbwjEAShTXNv2FaKQ5OiYjCItRwuob3S9cNBJliAe68/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1hbwjEAShTXNv2FaKQ5OiYjCItRwuob3S9cNBJliAe68/edit#gid=0"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": [
            {
              "id": "Invoice Number",
              "displayName": "Invoice Number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Supplier Name",
              "displayName": "Supplier Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Invoice Date",
              "displayName": "Invoice Date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Currency",
              "displayName": "Currency",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Base Amount (Original)",
              "displayName": "Base Amount (Original)",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "FX Rate (to INR)",
              "displayName": "FX Rate (to INR)",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Total Value (INR)",
              "displayName": "Total Value (INR)",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Treasury Approval Status",
              "displayName": "Treasury Approval Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Risk Category",
              "displayName": "Risk Category",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "c7bfafac-1b54-4d40-a85b-d5e0dc67ae1a",
      "name": "Log to Treasury Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1296,
        -112
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "LOs2dbk9lby0NfDM",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list"
        },
        "builtInTools": {},
        "options": {}
      },
      "id": "70a2630f-3040-466c-b5d8-8da1e62e696a",
      "name": "GPT-4o",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        592,
        96
      ],
      "credentials": {
        "openAiApi": {
          "id": "ICwxUBbatsF2sDvy",
          "name": "OpenAi account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Invoice Upload Trigger": {
      "main": [
        [
          {
            "node": "Download Invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Invoice": {
      "main": [
        [
          {
            "node": "LlamaIndex Upload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "LlamaIndex Upload": {
      "main": [
        [
          {
            "node": "Wait for Parsing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait for Parsing": {
      "main": [
        [
          {
            "node": "Check Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Status": {
      "main": [
        [
          {
            "node": "Status Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Status Switch": {
      "main": [
        [
          {
            "node": "Get Markdown",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait for Parsing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Markdown": {
      "main": [
        [
          {
            "node": "FX Invoice Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "FX Invoice Extractor": {
      "main": [
        [
          {
            "node": "Fetch Real-time FX",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Real-time FX": {
      "main": [
        [
          {
            "node": "FX Calculator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "FX Calculator": {
      "main": [
        [
          {
            "node": "Log to Treasury Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GPT-4o": {
      "ai_languageModel": [
        [
          {
            "node": "FX Invoice Extractor",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "0bb1497b-3716-47d7-a2f6-2a926eccfe78",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "3a43da28588548e21903e71cf1dc3ddd65c24bf0c62e7e4b77542ffe87ad79c6"
  },
  "id": "c26DLHSlEUU8A5wI",
  "tags": []
}